//
//  ZYNewThreeGoodStrongController.m
//  优学荟管理系统
//
//  Created by ちょうかんこん on 2018/1/11.
//  Copyright © 2018年 星道三好. All rights reserved.
//

#import "ZYNewThreeGoodStrongController.h"
#import "ZYNewThreeGoodStrongModel.h"
#import "ZYNewThreeGoodStrongCell.h"
#import "HNThreeGoodEvaVC.h"
#define kCell @"cell"
@interface ZYNewThreeGoodStrongController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;
@property (nonatomic,copy) NSString *searchStr;

@end

@implementation ZYNewThreeGoodStrongController


- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"zyNewStrong" object:nil];
    [self setUpSuperView];
    [self setUpRefresh];
}

- (void)setUpSuperView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 - 74)];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYNewThreeGoodStrongCell class] forCellReuseIdentifier:kCell];
    
    
    self.tableView.backgroundColor = ColorRGB(245, 245, 245);
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    
    
}


#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}










- (void)setNetWork{
    
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getTrainLessonList&pageNumber=%ld&oid=%@&type=1&searchName=%@&flag=2",(long)self.page,[AccountTool shareAccountTool].currentAccount.oid,self.searchStr?self.searchStr:@""];
    
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    NSString *changeUilStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(changeUilStr) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
            }
            
            NSDictionary *t = [Obj valueForKey:@"t"];
            NSArray *dataList = [t valueForKey:@"dataList"];
            
            for (NSDictionary *dict  in dataList) {
                
                ZYNewThreeGoodStrongModel *model = [ZYNewThreeGoodStrongModel new];
                
                model.ID = [dict valueForKey:@"id"];
                
                [model setValuesForKeysWithDictionary:dict];
                
                
                
                
                [self.dataSource addObject:model];
                
                
            }
            
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
        
        
        
    } Fail:^(NSError *Error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
}







#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYNewThreeGoodStrongCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYNewThreeGoodStrongModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYNewThreeGoodStrongModel:model];
        
        
    }
    
    cell.ClickDelete = ^(NSString *EnrollInfoId, NSString *ID) {
        [self deleteStudentWithID:ID errID:EnrollInfoId];
    };
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    
    return cell;
}

#pragma mark - 删除学生
- (void)deleteStudentWithID:(NSString *)ID errID:(NSString *)errID {
    //    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=delStudentNotValid&uid=%@&enrollInfoId=%@&type=1",ID,errID];
    
    NSString *url = @"trainLesson.do?action=delStudentNotValid";
    NSDictionary *params = @{
                             @"uid":ID,
                             @"enrollInfoId":errID,
                             @"type": @"1"
                             };
    //
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Post:LLBaseURL(url) Auth:YES Params:params Success:^(id Obj) {
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            [SVProgressHUD setStatus:Obj[@"msg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
                [self.tableView.mj_header beginRefreshing];
            });
        }
    } Fail:^(NSError *Error) {
        
        [SVProgressHUD dismiss];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZYNewThreeGoodStrongModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    HNThreeGoodEvaVC *VC = [HNThreeGoodEvaVC new];
    VC.ID = (NSNumber *)model.ID;
    VC.Name = model.name;
    VC.teacherId = model.uid;
    VC.HNThreeGoodEvaMode = HNThreeGoodEvaModeCourseList;
    [self.navigationController pushViewController:VC animated:YES];
    
    
    
    
    
}

- (void)nsnotificationAction:(NSNotification *)nsnotification{
    
    self.searchStr = [nsnotification valueForKey:@"object"];
    
    [self.tableView.mj_header beginRefreshing];
    
    
    
}



- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"zyNewStrong" object:nil];
    
    
    
}


- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}






@end
